Okay, so today we are looking at the last type of loop which are repeat loops.
Okay, so open up a script like last time and just start off by creating a variable for the part again.
Um, so part is equal to script dot parent.
Um, or let's um, also let's also add in a fire effect inside of the part, just to make this a bit
more interesting.
Okay.
And um, let's also reference that fire.
So is equal to part dot fire.
Okay.
So um, repeat loops are actually very, very similar to while loops.
Okay.
The reason why I'm showing you both of them is simply because both of them are used very commonly.
So really you only need to know, um, one of while loops or repeat loops because they do very similar
things.
Okay.
Um, but if you were um, if you have read up, read up some code online and you saw how had repeat
loop in it, you didn't know what it meant.
So it's a good idea just to know.
Anyway, even if, um, it might not be, you might be able to use while loops instead.
Or alternatively, if you found while loops a bit difficult, you might find repeat loops easier to
understand.
Um, so that's what we're looking at today.
So repeat loops.
How they work is they repeat the code that you write until a condition is met.
Okay.
And the key bit here is on repeating the code.
Any code you write in a repeat loop will automatically run once no matter what, because the condition
does not control if the code runs, but it controls if it gets repeated.
So, um, the we want to start off with by writing repeat.
But it's a different kind of format because, um, as we run the code once first and then we check for
the condition, if you press enter after that, you'll see you get a repeat, and then you get until
keyword at the end of it there.
Um, so basically your condition goes at the end because you want it to read your code first and then
check if it should repeat.
The whole point of it is, is whether it repeats, not if it runs.
If you're wanting to do it when it runs, you should be using a while loop.
So let's have a look at the fire here.
Okay.
Um, it currently has a as a this kind of color here.
Okay.
You can change this color from what it currently is to black, for example.
Um, so I thought, what what what might be quite cool is to repeat, changing the color of the fire
from maybe this color to another color.
So, um, let's write out the code in order to do that.
So fire dot color.
Now, even though you might not have touched on on you might have not coded some of the fire properties
before.
You'll know what you know, what the color property does for fire.
We've talked about that in the previous section okay.
And you also know how to change the color of parts.
So it's the same kind of things really.
You're just using a different object is a color free value.
So is equal to color free dot new.
And inside here we're going to give it the same color that it already is.
Um so if we so what we can do is um, there's a little, um, button here.
You can click the second button.
And if you click that, it actually brings up a color wheel for you.
And you can select the color you want from here, which could be easier in this case.
So let's pick a color which is similar to that.
And that's pretty much.
What it is.
Yeah.
And let's click okay.
And that's what it's given us for our color there okay.
After that let's then weight let's say one second and then change the color again.
But this time let's change it to a different kind.
Let's change it to purple.
Blue kind of color like that.
And then wait another second so it will change it to the first color it wait a second, change it to
a second color, wait a second.
And if the, um, if the condition is not has not been met, it will keep repeating it.
Okay.
So the condition we write after the until um, keyword, um, if this condition is met, it will stop
repeating the code.
But if a condition um, if a, if a condition is not not been met, it will just keep repeating it.
Okay.
Um, so let's say in this case, um, we want to keep doing this until we fire enabled property, um,
is turned off.
So once the fire becomes disabled.
So this means that the fire will no longer be visible.
It will stop.
It will stop, um, showing flames and things like that.
It'd be pointless changing the color if we can't see it.
So we can just stop it like that.
There.
Okay, so, um, we can say until fire, um, dot enabled and this is a boolean, so we could say is
equal equal false.
Or we could just say until not fire enabled like that's fair okay.
And this is repeat it until the fire has been disabled.
So let's try this out by running the game and having a look.
So we can see it changes our color like that, which I think is quite cool.
You could do it too.
Maybe visual.
Visual effects on a stage, perhaps, or something else like that.
Um, it's quite a cool script.
Okay, let's see what happens.
So let's click on the part, go into the fire, and if I disable it.
Event stops like that.
It took a minute because it has to.
It has to finish that bit of code.
Um, but it then stopped there, which is perfect.
Okay, cool.
So that's just a very simple example on how repeat loops work.
Okay.
Um, one other feature of repeat loops is that you can also make code repeat forever.
Okay.
Um, by saying repeat until false.
And how this works is, um.
Our condition.
We already know.
We've already spoken about conditions quite a lot in the last few lessons.
A condition has to be true in order for it to be met.
Okay, so that could be saying does this property equal this bit of data or is does does the anchored
property for the part equal to true.
Things like that.
Okay.
But false can never be true.
It's the complete opposite for it.
So if you say repeat until false this is our condition.
Our condition can never be true because it is literally the opposite.
It is false okay.
It is the Boolean value of false.
So this means you can write any code in here.
And it's actually repeats that code forever.
Okay.
Now typically you don't you don't do this too much in scripting.
Because when you repeat something forever for um, when you the kind of reader of the script will be
stuck in this repeat loop and it won't be able to read any, any more of your code going forward.
Okay.
Um, but you might find it useful as you're starting off because typically, um, you might use a repeat
loop or repeat until false.
So repeating it forever and then later find out, oh, I could use it.
I could do this way instead which doesn't involve this.
So it's a good thing to learn is a good thing to understand, but you will probably use it less as other
ways of doing this instead are a bit easier to use basically.
Um, and just to mention as well, I don't think I mentioned this in the while loops lesson, but you
can do the same for a while loop as well by saying while true, do like that they mean the exact same
thing.
Okay.
Apart from while true, do.
Um, we'll automatically run the code, whereas the repeat until false at the end until false bit will
be determining whether the code repeats.
Okay.
So there are repeat loops.
They are the last type of loop that we will look at.
For now in the course.
Okay so have a look at those types of loop.
Okay.
Next lesson we are going to continue looking at loops.
We last lesson on loops.
But we will be looking at how some of these loops actually they become most useful when you put them
inside of each other rather than using them individually.
And also look about a special case about how you can break loops and how that's important.
So join me in the next lecture when we have a look at that.